package org.traffometer.traffosim.gui.model;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Test;

/**
 * Tests the functions in the TestConfigModel class.
 * 
 * @author George Lee
 *
 */
public class TestConfigModel {
  /**Data that should pass the validation process.*/
  private static final String RATE = "1";
  
  private static final String LAT0 = "10";
  private static final String LONG0 = "32.44";
  private static final String TIME0 = "56778";

  private static final String LAT1 = "33.45";
  private static final String LONG1 = "170.23";
  private static final String TIME1 = "88883";

  private static final String VEHICLE = "foo-bar";

  /**String that should not pass the validation process.*/
  private static final String BAD_STRING = "foo";

  /** String that represents the status of the ConfigModel after adding a packet.*/
  private static final String ADD_PACKET_STATUS = "Number of packets to send: ";

  /** Directory path to write the configuration file to. */
  private static final String GUI_TEST = "build/guimodel-test.cfg";

  /** String that represents the status of the ConfigModel after saving.*/
  private final String saveStatus = "Configuration file saved to " + this.GUI_TEST;
  
  private static final String PARSE_ERROR = "Error in parsing data to create packet.";

  
  /**
   * Test creating a configuration file.
   *
   *@throws Exception if an error occurs during testing.
   */
  @Test
  public void testNormalConfig() throws Exception {
    ConfigModel testModel = ConfigModel.getInstance();

    //Check that initial status is empty.
    assertEquals("Testing initial status.", "", testModel.toString());

    //Add data to the ConfigModel.
    testModel.setBatchRate(RATE);
    testModel.setStartLat(LAT0);
    testModel.setStartLong(LONG0);
    testModel.setStartTime(TIME0);

    testModel.setEndLat(LAT1);
    testModel.setEndLong(LONG1);
    testModel.setEndTime(TIME1);

    testModel.setVehicle(VEHICLE);

    //Add packet to the ConfigModel.  Check to see that status is updated.
    testModel.addPacket();
    assertEquals("Testing addition of a packet.", ADD_PACKET_STATUS + 1, testModel
        .toString());

    // Write configuration file.
    testModel.saveFile(GUI_TEST);
    assertEquals("Testing file save.", this.saveStatus, testModel.toString());
  }
  
  /** 
   * Test adding bad data to the packet data.
   */
  @Test
  public void addBadData() {
    ConfigModel testModel = ConfigModel.getInstance();
    
    testModel.setBatchRate(RATE);
    
    //Add bad data to the ConfigModel.
    testModel.setStartLat(BAD_STRING);
    testModel.setStartLong(LONG0);
    testModel.setStartTime(TIME0);

    testModel.setEndLat(LAT1);
    testModel.setEndLong(LONG1);
    testModel.setEndTime(TIME1);

    testModel.setVehicle(VEHICLE);
    
    //Add packet to ConfigModel.  Check that status reports error.
    testModel.addPacket();
    assertEquals("Testing addition of bad data.", PARSE_ERROR, testModel.toString());
    assertEquals("Checking that no packet was added.", 0, testModel.numPackets());
  }
  
  /**
   * Remove all packets to ensure a fresh ConfigModel.
   */
  @After
  public void removePackets() {
    ConfigModel testModel = ConfigModel.getInstance();
    for (int i = 0; i < testModel.numPackets(); i++) {
      testModel.removePacket(i);
    }
  }
}
